package com.dwxt.cdms.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.dwxt.cdms.listener.LbWebSocketClient;
import com.dwxt.cdms.util.RedisUtil;
import com.dwxt.common.aop.ValidateAspectHandel;
import com.dwxt.common.config.FeignConfiguration;
import com.dwxt.common.exception.ExceptionHandler;
import com.dwxt.common.util.PartyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * 注入一些公共的处理类
 */
@Configuration
public class BeanConfig {

    @Bean
    public ValidateAspectHandel getValidateAspectHandel() {
        return new ValidateAspectHandel();
    }

    @Bean
    public PartyUtil getPartyUtil() {
        return new PartyUtil();
    }

    @Bean
    public ExceptionHandler getExceptionHandler() {
        return new ExceptionHandler();
    }

    @Bean
    public FeignConfiguration getFeignConfiguration(){
        return new FeignConfiguration();
    }

    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.secret}")
    private String secret;
    @Value("${aliyun.signName}")
    private String signName;
    @Value("${lb.websocketUri}")
    private String websocketUri;

    @Bean
    public IAcsClient getIAcsClient(){

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,secret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
            return new DefaultAcsClient(profile);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public SendSmsRequest getSendSmsRequest(){
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setMethod(MethodType.POST);
        //控制台创建的签名名称
        sendSmsRequest.setSignName(signName);
        return sendSmsRequest;
    }


    @Bean(name = "LbWebSocketClient")
    public LbWebSocketClient webSocketClient() throws URISyntaxException {
        LbWebSocketClient client = new LbWebSocketClient(new URI(websocketUri));
        client.connect();
        return client;
    }
}
